<?php
namespace app\admin\controller;
use app\common\controller\AdminController;
use app\common\model\Swiper as  swiperModel;
use app\common\model\Article as  articleModel;
use think\facade\Request;
use think\facade\View;


class Swiper extends AdminController {

    public function index(){
        if($this->request->isAjax()){
            $model = new swiperModel();
            $re['code'] = 0;
            $re['msg'] = '';
            $re['data'] = [];
            $re['count'] = 0;
            $res = $model->order('sort','desc')->paginate()->toArray();
            if(sizeof($res['data'])>0){
                $re['data'] = $res['data'];
                $re['count'] = $res['total'];
            }
            return $re;
        }else{
            return View::fetch();

        }
    }

    public function edit()
    {
        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        View::engine()->layout(false);
        $model = new swiperModel();
        if($this->request->isPost())
        {
            $data = $this->request->param();

            list($width, $height, $type) = getimagesize($data['image']);
            $data['width'] = $width;
            $data['height'] = $height;
            $domain = Request::domain();
            $data['path'] =PATH.explode($domain,$data['image'])[1];
            if($this->request->param('id')){
                unset($data['id']);
                $model->strict(false)->where('id',$this->request->param('id'))->save($data);
            }else{
                $model->strict(false)->insert($data);
            }

            $this->setActionLog('编辑轮播图',$this->admin_info['name'].'：编辑轮播图'.$this->request->param('id'));
            $status = [
                'status' => true,
                'msg' => '修改成功',
                'data' => []
            ];
            return  $status;
        }

        if($this->request->param('id'))
        {
            //编辑页面
            $info = $model->where(['id'=>$this->request->param('id')])->find();
            if(!$info)
            {
                return error_code(10000);
            }
            View::assign('data',$info);
        }
        $result['status'] = true;
        $result['msg'] = '修改成功';
        $result['data'] = View::fetch();
        return $result;
    }


    public function add(){

        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        View::engine()->layout(false);
        $model = new swiperModel();

        if($this->request->isPost())
        {
            $data = $this->request->param();
            $data['created_at'] = TIMESTAMP;
            $data['updated_at'] = TIMESTAMP;

            list($width, $height, $type) = getimagesize($data['image']);
            $data['width'] = $width;
            $data['height'] = $height;
            $domain = Request::domain();
            $data['path'] =PATH.explode($domain,$data['image'])[1];
            $id = $model->strict(false)->insertGetId($data);
            $this->setActionLog('添加轮播图',$this->admin_info['name'].'：添加轮播图'.$id);

            $status = [
                'status' => true,
                'msg' => '添加成功',
                'data' => []
            ];
            return  $status;
        }
        $result['status'] = true;
        $result['msg'] = '成功';
        $result['data'] = View::fetch();
        return $result;
    }


    public function del(){
        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        $id = $this->request->param('id');
        $model = new swiperModel();
        $res = $model->where('id',$id)->delete();
        if($res) {
            $this->setActionLog('删除轮播图',$this->admin_info['name'].'：删除轮播图'.$id);
            $result['status'] = true;
            $result['msg'] = '删除成功';
        }else{
            $result['msg'] = '删除失败，请联系管理员';
        }
        return $result;
    }

}
